//-----------------------------------------------------------------------
// <copyright file="CapacityDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CapacityDAOBase
    {
        #region Common methods
        public virtual Capacity CreateCapacityFromReader(IDataReader reader)
        {
            Capacity item = new Capacity();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CapacityID"))) item.CapacityID = (int)reader["CapacityID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOder"))) item.SortOder = (int)reader["SortOder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCapacityFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCapacity methods
            
        public virtual void CreateCapacity(Capacity capacity)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, capacity.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, capacity.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, capacity.Enable);
                database.AddOutParameter(dbCommand, "@CapacityID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                capacity.CapacityID = (int)database.GetParameterValue(dbCommand, "@CapacityID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCapacityException, ex);
            }
        }

        #endregion

        #region UpdateCapacity methods
        
        public virtual void UpdateCapacity(Capacity capacity)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityUpdate");            
                
                database.AddInParameter(dbCommand, "@CapacityID", DbType.Int32, capacity.CapacityID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, capacity.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, capacity.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, capacity.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCapacityException, ex);
            }
        }
        
        #endregion

        #region DeleteCapacity methods
        public virtual void DeleteCapacity(int capacityID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityDelete");
                
                database.AddInParameter(dbCommand, "@CapacityID", DbType.Int32, capacityID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCapacityException, ex);
            }
        }

        #endregion

        #region GetCapacity methods
        
        public virtual Capacity GetCapacity(int capacityID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityGet");
                
                database.AddInParameter(dbCommand, "@CapacityID", DbType.Int32, capacityID);
                
                Capacity capacity =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        capacity = CreateCapacityFromReader(reader);
                        reader.Close();
                    }
                }
                return capacity;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCapacityException, ex);
            }
        }
        
        #endregion

        #region GetCapacityList methods
        public virtual CapacityCollection GetCapacityList(CapacityColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CapacityCollection capacityCollection = new CapacityCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Capacity capacity = CreateCapacityFromReader(reader);
                        capacityCollection.Add(capacity);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return capacityCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCapacityListException, ex);
            }
        }
        
        public virtual CapacityCollection GetCapacityList(CapacityColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCapacityList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCapacityList methods
        public virtual CapacityCollection GetCapacityListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCapacityGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CapacityCollection capacityCollection = new CapacityCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Capacity capacity = CreateCapacityFromReader(reader);
                        capacityCollection.Add(capacity);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return capacityCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCapacityListException, ex);
            }
        }
        
        public virtual CapacityCollection GetCapacityListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCapacityListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}